package src;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import lucene.LuceneConstants;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;

public class IndexCluster {

	/**
	 * @param args
	 */
	private static IndexWriter iwriter;
	public IndexCluster(){
		try{
			Analyzer analyzer = new  StandardAnalyzer();
			iwriter = new IndexWriter(LuceneConstants.clusterindexdir, analyzer, true);
			iwriter.setMergeFactor(100);
		}
		catch(Exception e){
			e.printStackTrace();
			System.exit(-1);
		}
	}
	
	public void index(String variations, String head){
		System.out.println(head+" "+variations);
		try {
			Document doc = new Document();
			doc.add(new Field("k1",variations,Field.Store.YES,Field.Index.TOKENIZED));
			doc.add(new Field("h1",head,Field.Store.YES,Field.Index.UN_TOKENIZED));
			iwriter.addDocument(doc) ;
		} catch (CorruptIndexException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void indexData(String dir){
		
		File file = new File(dir);
		String files[] = file.list();
	
		for(int i=0; i<files.length; i++){
			File tfile = new File(dir+files[i]);
			if(tfile.isFile()){
				try {
					BufferedReader reader = new BufferedReader(new FileReader(tfile));
					String head = reader.readLine();
					head = head.replaceAll("\\n","");
					String variation = null;
					String vars = ""; 
					while((variation = reader.readLine())!=null){
						variation = variation.replaceAll("\\n","");
						vars = variation+" "+vars;
					}
					for(int j=0; j<3;j++){
						index(vars, head);
					}
					//index(vars, head);
					reader.close();
					
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e){
					e.printStackTrace();
				}
			}
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		IndexCluster ic = new IndexCluster();
		ic.indexData(LuceneConstants.clusterdatadir);
		System.out.println(iwriter.docCount());	
	}
}
